<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Main extends PNC_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('mod_employee');
	}

	public function index() {
		$this->login();
	}

//Login for User
	public function login() {
		$data['menu'] = $this->mod_menu->show_menu();
		$data['title'] = 'Log in';
		if (isset($_POST['btn_signin'])) {
			/* $this->form_validation->set_rules('field_input','label','required'); */
			$this->form_validation->set_rules('txt_username', 'Username', 'required|trim');
			$this->form_validation->set_rules('txt_password', 'Password', 'required|trim');
			if ($this->form_validation->run() == FALSE) {
				$this->session->set_userdata('login_erro', show_message('Your username or password is not match!', 'error'));
				redirect('main/');
                                    
			} else {
//                            echo "Hello"; exit();
				$username = $this->input->post('txt_username');
				$password = $this->input->post('txt_password');
				$this->session->set_userdata('old_pass', md5($password));

				$data['login'] = $this->mod_employee->login($username, $password);

				if ($this->check_session() || $data['login']) {
					if ($data['login']->num_rows() > 0) {
						foreach ($data['login']->result() as $rows) {
							$this->session->set_userdata('staff_id', $rows->staff_id);
							$this->session->set_userdata('e_mail', $rows->emp_email);
							$this->session->set_userdata('position', $rows->pos_name);
							$this->session->set_userdata('full_name', ucfirst($rows->emp_fname) . ' ' . strtoupper($rows->emp_lname));
							$this->session->set_userdata('id_user', $rows->staff_id);
							/* Remember username and password */
							if ($this->input->post('remember')) {
								$this->input->set_cookie('remem_user', $username, time() + (30 * 3600));
								$this->input->set_cookie('remem_pass', $password, time() + (30 * 3600));
							} else {
								delete_cookie('remem_user');
							} /* End of remember */

							$id = $rows->emp_id;
							if ($rows->role_alias === 'admin') {
								$this->session->set_userdata('admin', $id);
								redirect('admin/');
							} else if ($rows->role_alias === 'staff') {
								$this->session->set_userdata('staff', $id);
								redirect('staff/home');
							} else if ($rows->role_alias === 'submanager') {
								$this->session->set_userdata('submanager', $id);
								redirect('submanager/home');
							} else if ($rows->role_alias === 'manager') {
								$this->session->set_userdata('manager', $id);
								redirect('manager/home');
							} else if ($rows->role_alias === 'gm') {
								$this->session->set_userdata('gm', $id);
								redirect('gm/home');
							}
						}
					} else {
						$this->session->set_userdata('login_erro', show_message('Your username or password is not match!', 'error'));
						redirect('main/');
					}
				}
				redirect($this->session->userdata('previous_page'));
			}
		} else {
			$this->view_main($data);
		}
	}

	/* Reset password */

	public function reset_pass() {
		$data['title'] = 'Reset password';
		if (isset($_GET['info'])) {
			$data['info'] = $_GET['info'];
		}
		if (isset($_GET['error'])) {
			$data['error'] = $_GET['error'];
		}
		$this->view_main($data);
	}

	public function doforget() {
		$this->load->helper('url');
		$email = $this->input->post('email');
		if ($email === '') {
			$this->session->set_userdata('empty_mail', show_message('Please enter your email address!', 'error'));
			redirect('main/reset_pass', 'refresh');
		}
		$q = $this->db->query("select * from pnc_employee where emp_email='" . $email . "'");
		if ($q->num_rows > 0) {
			$r = $q->result();
			$user = $r[0];
			PNC_Controller::send_reset_password($user, $email);
		}
		$this->session->set_userdata('no_user', show_message("The email id you entered not found on our database.", 'error'));
		$this->session->set_userdata('sms_confirm', show_message("<center><font face='Verdana' size='2' color=red >There is some system problem in sending login details to your address. Please contact site-admin. <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>", 'warming'));
		
		redirect('main/reset_pass', 'refresh');
	}

	/* Send mail reset password */


}

/* End of file main.php */
/* Location: ./application/controllers/main.php */